package com.association.main.controller;

import com.association.main.common.R;
import com.association.main.domain.Department;
import com.association.main.service.DepartmentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @GetMapping
    public R<Page> departmentLists(int page,int pageSize,@RequestParam("name") String name){
        Page pageInfo = new Page(page,pageSize);
        LambdaQueryWrapper<Department> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(name!=null && name!= "",Department::getName,name);
        departmentService.page(pageInfo,lambdaQueryWrapper);
        return R.success(pageInfo);
    }
    @GetMapping("/list")
    public R<List<Department>> departmentList(){
        List<Department> list = departmentService.list();
        return R.success(list);
    }


    @PostMapping
    public R<String> add(@RequestBody Department department){
        boolean save = departmentService.save(department);
        return save == true ?R.success("添加成功"):R.error("添加失败，未知错误");
    }
    @PutMapping
    public R<String> update(@RequestBody Department department){
        boolean update = departmentService.updateById(department);
        return update == true ?R.success("修改成功"):R.error("修改失败，未知错误");
    }

    @DeleteMapping
    public R<String> delete(@RequestBody Department department){
        boolean delete = departmentService.removeById(department.getId());
        return delete == true ?R.success("删除成功"):R.error("添删除失败，未知错误");
    }



}
